package com.jueling.culture.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jueling.culture.core.AbstractService;
import com.jueling.culture.dto.dto.BoleAwardDTO;
import com.jueling.culture.dto.req.QueryVO;
import com.jueling.culture.enums.YesNoEnum;
import com.jueling.culture.mapper.AgentUserMapper;
import com.jueling.culture.mapper.BoleAwardMapper;
import com.jueling.culture.model.BoleAward;
import com.jueling.culture.model.CustUser;
import com.jueling.culture.service.BoleAwardService;
import com.jueling.culture.service.CustUserInfoService;
import com.jueling.culture.util.DateUtil;
import com.jueling.culture.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * @describe 伯乐奖
 * @author linp
 * @date  2022/04/11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BoleAwardServiceImpl extends AbstractService<BoleAward> implements BoleAwardService {

    @Resource
    @SuppressWarnings("unused")
    private BoleAwardMapper boleAwardMapper;
    @Resource
    private AgentUserMapper agentUserMapper;
    @Resource
    private CustUserInfoService custUserInfoService;

    /**
     * 伯乐奖领取
     *
     * @param bljId
     */
    @Override
    public void updateBoleAward(String bljId) {
        //修改领取状态
        boleAwardMapper.updateBoleAward(bljId,"lq");
    }

    /**
     * 伯乐奖结算
     *
     */
    @Override
    public void closeBoleAward() {
        //调用函数
        boleAwardMapper.dealWithBoleAward();
    }

    /**
     * 查询伯乐奖列表
     *
     * @param queryVO
     * @return
     */
    @Override
    public PageInfo<BoleAwardDTO> getBoleAwardList(QueryVO queryVO) {
        //判断是否需要级联查询
        if(YesNoEnum.YES.getCode().equals(queryVO.getIsCascade()) && StringUtil.isNotEmptyStr(queryVO.getAgentId())){
            CustUser custUser = custUserInfoService.getCustUserInfoByCustID(queryVO.getAgentId());
            //查询下级代理人
            String custId = agentUserMapper.getLowerCustId(custUser.getCustIdAll());
            if(StringUtil.isNotEmptyStr(custId)){
                List<String> arrayList = new ArrayList<String>();
                arrayList.addAll(Arrays.asList(custId.split(",")));
                arrayList.add(queryVO.getAgentId());
                queryVO.setAgentIdList(arrayList);
                //保留一条条件
                queryVO.setAgentId(null);
            }
        }
        PageHelper.startPage(queryVO.getPageIndex(), queryVO.getPageSize());
        List<BoleAwardDTO> list = boleAwardMapper.getBoleAwardList(queryVO);
        PageInfo<BoleAwardDTO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 获取全量伯乐奖信息
     *
     * @param queryVO
     * @return
     */
    @Override
    public List<List<Object>> getAllBoleAwardList(QueryVO queryVO) {
        //判断是否需要级联查询
        if(YesNoEnum.YES.getCode().equals(queryVO.getIsCascade())){
            //查询下级代理人
            String custId = agentUserMapper.getLowerCustId(queryVO.getAgentId());
            if(StringUtil.isNotEmptyStr(custId)){
                List<String> arrayList = new ArrayList<String>();
                arrayList.addAll(Arrays.asList(custId.split(",")));
                arrayList.add(queryVO.getAgentId());
                queryVO.setAgentIdList(arrayList);
                //保留一条条件
                queryVO.setAgentId(null);
            }
        }
        List<BoleAwardDTO> list = boleAwardMapper.getBoleAwardList(queryVO);
        return formatData(list);
    }

    /**
     * 数据格式化
     * @param result
     */
    public List<List<Object>> formatData(List<BoleAwardDTO> result){

        List<List<Object>> list = new ArrayList<>();
        int num = 0;
        //格式化数据,并填充数据
        for (BoleAwardDTO boleAward : result ) {
            num ++;
            List<Object> data = new ArrayList<>();
            //序号
            data.add(num);
            //数据
            data.add(boleAward.getCustId());
            data.add(boleAward.getCustNo());
            data.add(boleAward.getLxdh());
            data.add(DateUtil.dateToString(boleAward.getDjsj(),DateUtil.FORMAT_DATETIME));
            data.add(boleAward.getBlj());
            data.add(boleAward.getBljzz());
            data.add(boleAward.getFysfylq() == null ? "-" : boleAward.getFysfylq() == 1 ? "是" : "否");
            data.add(boleAward.getJszt() == null ? "-" : boleAward.getJszt() == 1 ? "是" : "否");
            data.add(boleAward.getSfxnyh() == null ? "-" : boleAward.getSfxnyh() == 1 ? "是" : "否");
            data.add(boleAward.getSjtjrMc());
            data.add(dljbType(boleAward.getDljbId()));
            data.add(stateType(boleAward.getYhzt()));
            data.add(boleAward.getTjrmc());
            data.add(boleAward.getTjrmcNew());
            data.add(boleAward.getKhhmc());
            data.add(boleAward.getKhm());
            data.add(boleAward.getYhkh());
            data.add(boleAward.getShdz());
            data.add(DateUtil.dateToString(boleAward.getJssj(),DateUtil.FORMAT_DATETIME));
            list.add(data);
        }
        return list;
    }

    private static Object stateType(Integer state) {
        if (state == 0) {
            return "待确认";
        }
        if (state == 1) {
            return "已确认";
        }
        if (state == 2) {
            return "停用";
        }
        if (state == 3) {
            return "撤销";
        }
        return null;
    }

    private static String dljbType(String state) {
        if(StringUtil.isEmptyStr(state)){
            return "-";
        }
        if (state.equals("jlwh0001")) {
            return "市级代理";
        }
        if (state.equals("jlwh0002")) {
            return "省级代理";
        }
        if (state.equals("jlwh0003")) {
            return "大区代理";
        }
        if (state.equals("jlwh0004")) {
            return "一星代理";
        }
        if (state.equals("jlwh0005")) {
            return "二星代理";
        }
        if (state.equals("jlwh0006")) {
            return "三星代理";
        }
        if (state.equals("jlwh0007")) {
            return "四星代理";
        }
        if (state.equals("jlwh0008")) {
            return "五星代理";
        }
        return null;
    }
}
